package ma.xin.shi.common;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
//import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.DialogCellEditor;
import java.util.ArrayList;
import java.util.List;

/**
 * 多选弹窗 Cell Editor，用于表格中支持多选的列。
 */
public class MultiSelectDialogCellEditor extends DialogCellEditor {

    private List<String> selectedItems = new ArrayList<>();
    private String[] availableOptions;

    public MultiSelectDialogCellEditor(org.eclipse.swt.widgets.Table parent, String[] options) {
        super(parent);
        this.availableOptions = options;
    }

    @Override
    protected Object openDialogBox(Control cellEditorWindow) {
        Shell shell = new Shell(cellEditorWindow.getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell.setText("选择配置项");
        shell.setLayout(new GridLayout());

        CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(shell, SWT.BORDER);
        viewer.setContentProvider(ArrayContentProvider.getInstance());
        viewer.setLabelProvider(new LabelProvider());
        viewer.setInput(availableOptions);

        // 设置默认值
        for (String item : availableOptions) {
            viewer.setChecked(item, selectedItems.contains(item));
        }

        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        gd.widthHint = 300;
        gd.heightHint = 200;
        viewer.getControl().setLayoutData(gd);

        CustomDialog dialog = new CustomDialog(shell, viewer, availableOptions, selectedItems);
        if (dialog.open() == IDialogConstants.OK_ID) {
            selectedItems.clear();
            for (Object obj : availableOptions) {
                if (viewer.getChecked(obj)) {
                    selectedItems.add((String) obj);
                }
            }
        }

        return String.join(", ", selectedItems); // 返回逗号分隔字符串用于显示
    }

    public void setSelectedItems(String[] items) {
        this.selectedItems = items != null ? new ArrayList<>(List.of(items)) : new ArrayList<>();
    }

    @Override
    protected void updateContents(Object value) {
        super.updateContents(value);
        if (value instanceof String[]) {
            selectedItems = new ArrayList<>(List.of((String[]) value));
        } else if (value instanceof List<?>) {
            @SuppressWarnings("unchecked") // 明确告诉编译器我们知道自己在做什么
            List<String> stringList = (List<String>) value;
            selectedItems = new ArrayList<>(stringList);
        } else if (value instanceof String) {
            selectedItems = new ArrayList<>(List.of(value.toString().split(",\\s*")));
        }
    }

    // 自定义内部 Dialog 类
    private static class CustomDialog extends Dialog {

        private CheckboxTableViewer viewer;
        //private String[] availableOptions;
        //private List<String> selectedItems;

        public CustomDialog(Shell parentShell, CheckboxTableViewer viewer,
                            String[] availableOptions, List<String> selectedItems) {
            super(parentShell);
            this.viewer = viewer;
            //this.availableOptions = availableOptions;
            //this.selectedItems = selectedItems;
        }

        @Override
        protected Control createDialogArea(Composite parent) {
            Composite container = (Composite) super.createDialogArea(parent);
            viewer.getControl().setParent(container);
            return container;
        }

        @Override
        protected void createButtonsForButtonBar(Composite parent) {
            createButton(parent, IDialogConstants.OK_ID, "确定", true);
            createButton(parent, IDialogConstants.CANCEL_ID, "取消", false);
        }
    }
}
